<?php

// src/TableTennis/PartnerBundle/DataFixtures/ORM/LoadPartnerData.php
namespace TableTennis\PartnerBundle\DataFixtures\ORM;

use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\Common\DataFixtures\FixtureInterface;
use TableTennis\PartnerBundle\Entity\Partner;

class LoadPartnerData implements FixtureInterface
{
    public function load(ObjectManager $manager)
    {
        require_once(__DIR__.'/../../../../../external_scripts/DB.php');
        require_once(__DIR__.'/../../../../../external_scripts/load_partner.php');

        foreach( $dataList as $data )
        {
            if( !empty( $data["creation_user_id"] ) ) {
                $creationUser = $manager->getRepository('Application\Sonata\UserBundle\Entity\User')->find( $data["creation_user_id"] );
            }
            if( !empty( $data["modification_user_id"] ) ) {
                $modificationUser = $manager->getRepository('Application\Sonata\UserBundle\Entity\User')->find( $data["modification_user_id"] );
            }

            $partner = new Partner();
            $partner->setId( $data["id"] );
            // $partner->setMediaId();
            if( isset( $creationUser ) ) {
                $partner->setCreationUserId( $creationUser );
            }
            if( isset( $modificationUser ) ) {
                $partner->setModificationUserId( $modificationUser );
            }
            $partner->setTitle( $data["title"] );
            $partner->setLink( $data["link"] );
            $partner->setContent( $data["content"] );
            $partner->setIsPublished( $data["publication"] == "published" ? TRUE : FALSE );
            // $club->setSlug();
            $partner->setCreatedAt( new \DateTime($data["created_at"]) );
            $partner->setUpdatedAt( new \DateTime($data["updated_at"]) );


            $manager->persist($partner);

            /* Pour forcer l'id */
            $metadata = $manager->getClassMetaData(get_class($partner));
            $metadata->setIdGeneratorType(\Doctrine\ORM\Mapping\ClassMetadata::GENERATOR_TYPE_NONE);

            $manager->flush();
        }
    }

    /**
     * The order in which these fixtures will be loaded.
     */
    public function getOrder()
    {
        return 3;
    }
}

?>